#!/bin/bash
###
# @Author: admin@attacker.club
# @Date: 2022-07-30 18:03:20
# @LastEditTime: 2024-08-09 11:45:38
# @Description:
###
# curl  -s  http://wuyou.run/services/oraclejdk11.sh| bash

# 检查是否已安装在 /usr/local/jdk-11 目录
if [ -d /usr/local/jdk-11 ]; then
    echo "已安装，退出安装"
    exit 0
fi

# 检查文件是否存在
if [ ! -f jdk-11_linux-x64_bin.tar.gz ]; then
    # 如果不存在，使用 wget 下载
    wget https://repo.huaweicloud.com/java/jdk/11+28/jdk-11_linux-x64_bin.tar.gz
    # 检查下载是否成功
    if [ $? -ne 0 ]; then
        echo "下载失败，退出安装"
        exit 1
    fi
fi

# 解压（假设解压到 /usr/local 目录下）
tar -zxvf jdk-11_linux-x64_bin.tar.gz -C /usr/local
ln -s /usr/local/jdk*/bin/java /usr/bin/java

# 检查是否存在 JAVA_HOME 环境变量
if grep -q "JAVA_HOME" /etc/profile; then
    echo "JAVA_HOME 环境变量已存在，无需添加"
else
    echo "export JAVA_HOME=/usr/local/jdk-11" >>/etc/profile
    echo "export CLASSPATH=\$JAVA_HOME/lib:\$CLASSPATH" >>/etc/profile
    echo "export JRE_HOME=\$JAVA_HOME/jre" >>/etc/profile
    echo "export PATH=\$JAVA_HOME/bin:\$PATH" >>/etc/profile
fi

# 使环境变量生效
source /etc/profile && java -version
